﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class ChargesRegister : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SqlConnection MyConnetion;
            SqlCommand MyCommand;
            SqlParameter usernameParam;
            SqlParameter passwordParam;

            MyConnetion = new SqlConnection();
            MyConnetion.ConnectionString = ConfigurationManager.ConnectionStrings["TelecomChargesConnectionString"].ConnectionString;

            MyCommand = new SqlCommand();
            MyCommand.CommandText = "insert into telecom_account (username,password,account_type) values (@username,@password,1)";
            MyCommand.CommandType = CommandType.Text;
            MyCommand.Connection = MyConnetion;

            usernameParam = new SqlParameter();
            usernameParam.ParameterName = "@username";
            usernameParam.SqlDbType = SqlDbType.VarChar;
            usernameParam.Size = 10;
            usernameParam.Direction = ParameterDirection.Input;
            usernameParam.Value = TextBoxUserName.Text;

            passwordParam = new SqlParameter();
            passwordParam.ParameterName = "@password";
            passwordParam.SqlDbType = SqlDbType.VarChar;
            passwordParam.Size = 10;
            passwordParam.Direction = ParameterDirection.Input;
            passwordParam.Value = TextBoxPassWord.Text;

            MyCommand.Parameters.Add(usernameParam);
            MyCommand.Parameters.Add(passwordParam);

            MyCommand.Connection.Open();
            int affectRowsNum = MyCommand.ExecuteNonQuery();

            MyCommand.Dispose();
            MyConnetion.Dispose();
            Response.Redirect("default.aspx");
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        SqlConnection MyConnetion;
        SqlCommand MyCommand;
        SqlDataReader MyReader;
        SqlParameter usernameParam;

        MyConnetion = new SqlConnection();
        MyConnetion.ConnectionString = ConfigurationManager.ConnectionStrings["TelecomChargesConnectionString"].ConnectionString;

        MyCommand = new SqlCommand();
        MyCommand.CommandText = "select username from telecom_account where username = @username";
        MyCommand.CommandType = CommandType.Text;
        MyCommand.Connection = MyConnetion;

        usernameParam = new SqlParameter();
        usernameParam.ParameterName = "@username";
        usernameParam.SqlDbType = SqlDbType.VarChar;
        usernameParam.Size = 10;
        usernameParam.Direction = ParameterDirection.Input;
        usernameParam.Value = args.Value;

        MyCommand.Parameters.Add(usernameParam);

        MyCommand.Connection.Open();
        MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);

        args.IsValid = !(MyReader.HasRows);

        MyCommand.Dispose();
        MyConnetion.Dispose();
    }
}
